package cn.edu.cqut.controller;


import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.edu.cqut.entity.Record;
import cn.edu.cqut.service.IRecordService;
import cn.edu.cqut.util.PageResult;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cqut-917371
 * @since 2020-06-24
 */
@RestController
@CrossOrigin  //允许跨域请求 
@RequestMapping("/record")
public class RecordController {
	@Autowired
	private IRecordService recordService;

	@RequestMapping("/updateRecord")
	public PageResult<Record> updateRecord(Record record) {
		recordService.updateById(record);
		PageResult<Record> pr = new PageResult<>();
		pr.setCode(0);
		pr.setMsg("修改交往记录信息成功");
		return pr;
	}

	
	 @RequestMapping("/addRecord") 
	 public PageResult<Record> addRecord(Record record){ 
		 recordService.save(record);
	     PageResult<Record> pr = new PageResult<>(); 
	     pr.setCode(0);
	     pr.setMsg("新增交往记录成功"); 
	     return pr; 
	}
	  
	@RequestMapping("/delRecord") 
	public PageResult<Record> delRecord(Integer[] ids){ 
		 recordService.removeByIds(Arrays.asList(ids));
	     PageResult<Record> pr = new PageResult<>(); 
	     pr.setCode(0);
	     pr.setMsg("删除交往记录成功"); 
	     return pr; 
	}
	
	@RequestMapping("/getCustomerRecord")
	public PageResult<Record> getCustomerRecord(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit,Record record) {
		QueryWrapper<Record> queryWapper = new QueryWrapper<>();
		queryWapper.apply("record.cus_no=customer.cus_no");
		if (record.getCusNo() != null) {
			queryWapper.eq("record.cus_no", record.getCusNo());
		}
		Page<Record> page3 = new Page<Record>(page, limit);
		Page<Record> page2 = recordService.getRecordWithCustomer(page3, queryWapper);
		PageResult<Record> pr = new PageResult<>();
		pr.setCode(0);
		pr.setCount(page2.getTotal());//返回符合条件的条数
		pr.setMsg("");
		pr.setData(page2.getRecords());// 返回当前页
		return pr;
	}
}

